package com.yuntsg.nnsfcp.service.front.ut;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.profile.DefaultProfile;
import com.yuntsg.nnsfcp.util.MD5;
import com.yuntsg.nnsfcp.util.httpclient.HttpClient;
import com.yuntsg.nnsfcp.util.httpclient.HttpClientConfig;
import com.yuntsg.nnsfcp.util.httpclient.HttpClientUtil;
import org.apache.commons.lang.StringUtils;
import org.apache.http.impl.client.BasicCookieStore;

import java.util.HashMap;
import java.util.Map;
import java.util.Random;

/**
 * 阿里大鱼 发送短信
 *
 * @author ：ljx
 * @date ：Created in 2019/9/19
 */
public class MsgUtil {

    /**
     * 发送短信
     *
     * @param phone 手机号
     * @return
     */
    public static String generatorSms(String phone, String template) {

        if (phone.equals("15676767671")) {
            return "1234";
        }

        if (StringUtils.isEmpty(template)) {

            //一个通用模板
            template = "SMS_162198586";
        }
        //String typetTemplate = "SMS_162198586";
        String code = generatorCode(); //随机验证码
        DefaultProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAILDbNKzIkvUhK", "s8Q4sBlLXSXKWOpxpG6rQOVPV1z0cD");
        IAcsClient client = new DefaultAcsClient(profile);
        CommonRequest request = new CommonRequest();
        request.setMethod(MethodType.POST);
        request.setDomain("dysmsapi.aliyuncs.com");
        request.setVersion("2017-05-25");
        request.setAction("SendSms");
        request.putQueryParameter("RegionId", "cn-hangzhou");
        request.putQueryParameter("PhoneNumbers", phone);
        request.putQueryParameter("SignName", "上海骥灏");
        request.putQueryParameter("TemplateCode", template);
        request.putQueryParameter("TemplateParam", "{\"code\":\"" + code + "\"}");
        try {
            CommonResponse response = client.getCommonResponse(request);
            JSONObject jsonObject = JSON.parseObject(response.getData());
            String reCode = (String) jsonObject.get("Code");
            if ("OK".equals(reCode)) {

            } else {
                code = "0000";
            }
        } catch (ServerException e) {
            e.printStackTrace();
            code = "0000";
        } catch (ClientException e) {
            e.printStackTrace();
            code = "0000";
        }
        return code;
    }


    //2020年9月14日19:00:58  换成情报所大学图书馆短信地址
    public static String generatorJNDXSms(String phone) {
        String code = generatorCode();
        String url = "https://libapi.jnu.edu.cn/libapi/auth/sms/sendMessage";
        String appid = "chaxin";
        String secret = "ff80808172499f9a01729d202a1c0002";
        String currentYmdStr = DateUtil.longToDate_ymdhms();
        String md5Code = MD5.string2MD5(secret + currentYmdStr + appid);

        Map paramMap = new HashMap();
        String hashcode = "您的短信验证码:" + code; //随机验证码

        paramMap.put("mobile", phone);
        paramMap.put("content", hashcode);
        paramMap.put("appid", appid);
        paramMap.put("seccode", md5Code);
        try {
            HttpClientConfig clientConfig = HttpClientUtil.getSearConfig(url, "", "", false);
            String s1 = HttpClient.postHtml(new BasicCookieStore(), paramMap, clientConfig);
            System.out.println(s1);
        } catch (Exception e) {
            code = "0000";
        }
        return code;

    }


    /**
     * 生成随机数
     *
     * @return
     */
    private static String generatorCode() {
        String str = "0123456789";
        StringBuilder sb = new StringBuilder(6);
        for (int i = 0; i < 6; i++) {
            char ch = str.charAt(new Random().nextInt(str.length()));
            sb.append(ch);
        }
        return sb.toString();
    }

    /**
     * main 测试
     *
     * @param args
     */
    public static void main(String[] args) {
        generatorSms("18678297353", "");
    }
}
